package com.assignwork.modules.mobile;

import cn.hutool.core.util.NumberUtil;
import com.assignwork.dto.LineAuditDTO;
import com.assignwork.dto.ListLiquidLevelInput;
import com.assignwork.dto.ListTideLevelInput;
import com.assignwork.entity.LiquidLevel;
import com.assignwork.entity.TideDataSszx;
import com.assignwork.entity.TideLevel;
import com.assignwork.dto.WorkLineLogDTO;
import com.assignwork.modules.mobile.service.IMobileService;
import com.assignwork.service.*;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("mobile")
public class MobileController {

    IMobileService mobileService;
    IDailyPlanService dailyPlanService;
    IWorkArrangeShipService workArrangeShipService;
    IWorkArrangeLandService workArrangeLandService;
    ILiquidLevelService liquidLevelService;
    ITideLevelService tideLevelService;
    IAssignWorkService assignWorkService;
    IWorkLineLogService workLineLogService;
    ITideDataSszxService tideDataSszxService;

    @GetMapping("list-daily-plan-mobile")
    public R listDailyPlan4Mobile(Query query, LineAuditDTO dto) {
        return R.data(dailyPlanService.listDailyPlan4Mobile(Condition.getPage(query), dto));
    }

    @GetMapping("list-work-arrange-mobile")
    public R listWorkArrange4Mobile(Query query, LineAuditDTO dto) {
        return R.data(workArrangeShipService.listWorkArrange4Mobile(Condition.getPage(query), dto));
    }

    @GetMapping("list-liquid-level")
    public R listLiquidLevel(ListLiquidLevelInput dto) {
        List<LiquidLevel> data = liquidLevelService.lambdaQuery()
                .ge(LiquidLevel::getCreateTime, dto.getBeginTime())
                .le(LiquidLevel::getCreateTime, dto.getEndTime())
                .orderByAsc(LiquidLevel::getCreateTime)
                .list();
        data = data.stream().filter(p -> p.getCreateTime().getMinutes() == 0).map(p -> {
            p.getCreateTime().setSeconds(0);
            return p;
        }).collect(Collectors.toList());
        return R.data(data);
    }

    @GetMapping("list-tide-data-sszx")
    public R tideDataSszx(ListLiquidLevelInput dto) {
        List<TideDataSszx> data = tideDataSszxService.lambdaQuery()
                .ge(TideDataSszx::getCreateTime, dto.getBeginTime())
                .le(TideDataSszx::getCreateTime, dto.getEndTime())
                .orderByAsc(TideDataSszx::getCreateTime)
                .list();
        data = data.stream().filter(p -> p.getCreateTime().getMinutes() == 0).map(p -> {
            p.getCreateTime().setSeconds(0);
            return p;
        }).collect(Collectors.toList());
        return R.data(data);
    }

    @GetMapping("real-liquid-level")
    public R RealLiquidLevel() {
        LiquidLevel data = liquidLevelService.lambdaQuery()
                .orderByDesc(LiquidLevel::getCreateTime)
                .last("limit 1")
                .one();
        return R.data(data);
    }

    @GetMapping("list-tide-level")
    public R listTideLevel(ListTideLevelInput dto) {
        List<TideLevel> data = tideLevelService.lambdaQuery()
                .ge(TideLevel::getCreateTime, dto.getBeginTime())
                .le(TideLevel::getCreateTime, dto.getEndTime())
                .orderByAsc(TideLevel::getCreateTime)
                .list();
        data.forEach(p -> {
            p.setValue(NumberUtil.div(p.getValue(), 100, 2));
        });
        return R.data(data);
    }

    @GetMapping("list-work-arrange-machine")
    public R listWorkArrangeMachine(LineAuditDTO dto) {
        return R.data(assignWorkService.listAssignWorkByArrangeId(dto.getArrangeId()));
    }

    @PostMapping("save-or-update-work-line-log")
    public R saveOrUpdateWorkLineLog(@RequestBody WorkLineLogDTO dto) {
        return R.data(workLineLogService.saveOrUpdateWorkLineLog(dto));
    }

    @GetMapping("work-line-log-by-arrange-id")
    public R workLineLogByArrangeId(String arrangeId) {
        return R.data(workLineLogService.workLineLogByArrangeId(Func.toLong(arrangeId), ""));
    }

    @PostMapping("audit-work-line-log")
    public R auditWorkLineLog(@RequestBody WorkLineLogDTO dto) {
        return R.data(workLineLogService.auditWorkLineLog(dto));
    }

    @GetMapping("list-land-work-arrange-mobile")
    public R listLandWorkArrangeMobile(Query query, LineAuditDTO dto) {
        return R.data(workArrangeLandService.listLandWorkArrangeMobile(Condition.getPage(query), dto));
    }

}
